package com.jxs.android.sxyw;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jxs.android.sxyw.data.CaseInfo;

public class CaseAdapter extends BaseAdapter {

	private List<CaseInfo> caseInfos = new ArrayList<CaseInfo>();
	private AbstractActivity context;
	private WeakReference<AbstractActivity> revokeActivity;

	private LayoutInflater inflater = null;
	private ViewHolder holder = null;

	public void addCaseInfos(List<CaseInfo> contacts) {
		this.caseInfos.clear();
		this.caseInfos.addAll(contacts);
	}

	public List<CaseInfo> getCaseInfos() {
		return caseInfos;
	}

	public CaseAdapter(AbstractActivity revokeActivity) {
		init(revokeActivity);
	}

	private void init(AbstractActivity revokeActivity) {
		this.revokeActivity = new WeakReference<AbstractActivity>(revokeActivity);
		context = this.revokeActivity.get();
		inflater = (LayoutInflater) revokeActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		return caseInfos.size();
	}

	public CaseInfo getItem(int position) {
		return caseInfos.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		final CaseInfo taskInfo = caseInfos.get(position);
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.eoms_list_item, null);
			holder = new ViewHolder();
			holder.item_icon = (ImageView) convertView.findViewById(R.id.item_icon);
			holder.title_tv = (TextView) convertView.findViewById(R.id.item_title);
			holder.end_time_tv = (TextView) convertView.findViewById(R.id.item_time);
			holder.state_tv = (TextView) convertView.findViewById(R.id.item_state);
			holder.list_menu = (ImageView) convertView.findViewById(R.id.item_arrow);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.item_icon.setImageResource(R.drawable.icon_eoms_item);
		holder.title_tv.setText(taskInfo.getTitle());
		holder.end_time_tv.setText(taskInfo.getOccur_time());
		holder.state_tv.setText(taskInfo.getStatus_name());
		OnClickListener onClickListener = new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onclickView(position, taskInfo);
			}
		};

		holder.list_menu.setOnClickListener(onClickListener);
		convertView.setOnClickListener(onClickListener);
		if (position % 2 == 0) {
			convertView.setBackgroundResource(R.drawable.roundlist_body_gray);
		} else {
			convertView.setBackgroundResource(R.drawable.roundlist_body);
		}
		return convertView;
	}

	private void onclickView(int position, CaseInfo caseInfo) {
		Intent intent = new Intent(context,CaseDetail.class);
		Bundle bundle = new Bundle();
		bundle.putSerializable("caseInfo", caseInfo);
		bundle.putInt("position", position);
		intent.putExtras(bundle);
		context.startActivityForResult(intent, CaseDetail.GET_DETAIL);
	}

	class ViewHolder {
		ImageView item_icon;// Item中的图片
		TextView title_tv;// Index
		TextView end_time_tv;// 昵称
		TextView state_tv;// 状态
		ImageView list_menu;// 数量
	}

}